package com.atguigu.gmall.web.all.controller;

import com.atguigu.gmall.feign.search.SkuEsFeignClient;
import com.atguigu.gmall.model.list.SearchParam;
import com.atguigu.gmall.model.list.SearchResponseVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

import java.security.cert.PKIXBuilderParameters;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 检索
 * 检索会发请求
 *      keyword=iphone
 *      trademark=2苹果
 *      props=23
 *      props=291
 *      category3Id
 *1、web-all： 将会收到来自页面的检所请求，而且带来很多数据SearchParam
 * 2、web-all: 根据带来的所有条件，调用远程的检索服务
 *
 * @author dyqstart
 * @create 2021-08-13-19:48
 * @since 1.0.0
 */
@Controller
@Slf4j
public class SearchListController {

    @Autowired
    SkuEsFeignClient skuEsFeignClient;

    @GetMapping("/list.html")
    public String search(SearchParam searchParam, Model model){

        //1、远程调用检索服务
        log.info("检索携带的参数：{}",searchParam);

        //2、根据请求不同条件，让检索服务进行检索
        Map<String, Object> result = skuEsFeignClient.searchGood(searchParam);
        //远程查询到的所有数据放进去
        //trademarkList\attrsList\goodsList\total\pageSize\pageNo\totalPages
        model.addAllAttributes(result);

        //检索条件
        model.addAttribute("searchParam",searchParam);


        String urlParam = buildUrlParam(searchParam);
        model.addAttribute("urlParam",urlParam);
        //检索页面需要的数据
        // trademarkList: 品牌列表
        // attrsList: 属性列表
        // goodsList: 商品集合
        //total;//总记录数
        //pageSize;//每页显示的内容
        //pageNo;//当前页面
        //totalPages;

        //回显排序规则 orderMap  type/sort
        Map<String,Object> orderMap = buildOrderMap(searchParam);

        model.addAttribute("orderMap",orderMap);

        //构建品牌的面包屑
        String trade = buildTrademarkParam(searchParam);
        model.addAttribute("trademarkParam",trade);

        //构建属性的面包屑 attrId/attrName/attrValue
        List<Map<String,Object>> propsParamList = buildPropsParamList(searchParam);
        model.addAttribute("propsParamList",propsParamList);



        return "list/index";
    }

    private List<Map<String, Object>> buildPropsParamList(SearchParam searchParam) {
        ArrayList<Map<String, Object>> list = new ArrayList<>();
        //构建属性的面包屑 attrId/attrName/attrValue
        if (searchParam.getProps() != null && searchParam.getProps().length > 0){
            for (String prop : searchParam.getProps()) {
                String[] split = prop.split(":");
                HashMap<String, Object> propMap= new HashMap<>();
                propMap.put("attrId",split[0]);
                propMap.put("attrName",split[2]);
                propMap.put("attrValue",split[1]);

                list.add(propMap);
            }
        }
        return list;
    }

    private String buildTrademarkParam(SearchParam searchParam) {
        if (!StringUtils.isEmpty(searchParam.getTrademark())){
            //trademark1小米
            return "品牌:"+searchParam.getTrademark().split(":")[1];
        }
        return null;
    }

    private Map<String, Object> buildOrderMap(SearchParam searchParam) {
        HashMap<String, Object> orderMap = new HashMap<>();
        if (!StringUtils.isEmpty(searchParam.getOrder())){
            String[] split = searchParam.getOrder().split(":");
            orderMap.put("type",split[0]);
            orderMap.put("sort",split[1]);
            return orderMap;
        }else {
            orderMap.put("type","1");
            orderMap.put("sort","desc");
            return orderMap;
        }
    }

    private String buildUrlParam(SearchParam searchParam) {

        StringBuilder builder = new StringBuilder();

        if (!StringUtils.isEmpty(searchParam.getKeyword())){
            builder.append("&keyword="+searchParam.getKeyword());
        }

        if (!StringUtils.isEmpty(searchParam.getCategory3Id()+"")){
            builder.append("&category3Id="+searchParam.getCategory3Id());
        }

        if (!StringUtils.isEmpty(searchParam.getTrademark())){
            builder.append("&trademark="+searchParam.getTrademark());
        }

        if (searchParam.getProps()!=null && searchParam.getProps().length > 0){
            for (String prop : searchParam.getProps()) {
                builder.append("&props="+prop);
            }
        }

//        if (!StringUtils.isEmpty(searchParam.getOrder())){
//            builder.append("&order="+searchParam.getOrder());
//        }

//        if (searchParam.getPageNo()!=null){
//            builder.append("&pageNo="+searchParam.getPageNo());
//        }
//
//        if (searchParam.getPageSize()!=null){
//            builder.append("&pageSize="+searchParam.getPageSize());
//        }

        String s = builder.toString();
        return "list.html?"+s;
    }
}
